﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Lime;

namespace Clandestine.Graphics
{
	interface IHasTransform2D
	{
		Transform2D Transform { get; set; }
	}

	public class Transform2D
	{
		public virtual float Rotation { get; set; }

		public virtual Point Origin { get; set; }
		public virtual int X { get; set; }
		public virtual int Y { get; set; }

		public virtual float ScaleUniform { get; set; }
		public virtual float ScaleX { get; set; }
		public virtual float ScaleY { get; set; }

		/// <summary>
		/// Dictates whether the X and Y coordinates are relative to the world (false) or the camera (true).  Default is false, use true for GUI and HUD stuff!
		/// </summary>
		public virtual bool GlueToCamera { get; set; }

		public Transform2D()
		{
			Rotation = 0f;
			Origin = Point.Empty;
			X = 0;
			Y = 0;
			ScaleUniform = 1f;
			ScaleX = 1f;
			ScaleY = 1f;
			GlueToCamera = false;
		}

		internal virtual void BeginTransformed()
		{
			// Save state...
			GL.PushMatrix();

			if (GlueToCamera)
				GL.LoadIdentity();

			//GL.Translatef(X, Y, 0f);
			GL.Translate(X - Origin.X, Y - Origin.Y, 0f);
			GL.Scale(ScaleUniform * ScaleX, ScaleUniform * ScaleY, 1f);
			GL.Rotate(Rotation, 0f, 0f, 1f);
		}

		internal virtual void EndTransformed()
		{
			// ...aaand restore!
			GL.PopMatrix();
		}
	}
}
